package GUI;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import client.ClientConsole;

import Library.ListOrderBookStudent;
import Library.OrderBookStudent;

/**
 * This class is the show order Book
 */ 
public class GUI_ShowOrderBook extends JPanel{
	/**
	 * client Connected
	 */
	private ClientConsole clc;
	/**
	 * table
	 */
	private JTable table;
	/**
	 * list for books
	 */
	private ArrayList<OrderBookStudent> listOrderBookStudent1;
	/**
	 * panel for show order Book
	 */
	private GUI_ShowOrderBook sob = this;
	/**
	 * panel for back
	 */
	private JPanel backPanel; 
	/**
	 * Text field 
	 */
	private JTextField textField;
	/**
	 * casting for readerId 
	 */
	private String rId;

	/**
	 * @param cc
	 * @param listOrderBookStudent 
	 * @param guiLoan 
	 * @param readerId 
	 */
	public GUI_ShowOrderBook( ClientConsole cc, ArrayList<OrderBookStudent> listOrderBookStudent, final GUI_loan guiLoan,String readerId) {
		
		setLayout(null);
		//this.userName=userName;
		this.setSize(600, 400);
		clc=cc;
		listOrderBookStudent1=listOrderBookStudent;
		backPanel=guiLoan;
		rId=readerId;
		same();
		forlibrerian();
	}		
	/**
	 * @param cc
	 * @param listOrderBookStudent 
	 * @param src 
	 */
	public GUI_ShowOrderBook( ClientConsole cc, ArrayList<OrderBookStudent> listOrderBookStudent, final Gui_ShowReaderCard src) {
		
		setLayout(null);
		//this.userName=userName;
		this.setSize(600, 400);
		clc=cc;
		listOrderBookStudent1=listOrderBookStudent;
		backPanel=src;
		same();
		showResults();
	}		
	
	/**
	 * @param cc
	 * @param listOrderBookStudent 
	 * @param back 
	 */
	public GUI_ShowOrderBook( ClientConsole cc, ArrayList<OrderBookStudent> listOrderBookStudent, final JPanel back) {
	
				setLayout(null);
				//this.userName=userName;
				this.setSize(600, 600);
				clc=cc;
				listOrderBookStudent1=listOrderBookStudent;
				backPanel=back;
				same();
				forStudent();
				
	}
	
	public void same()
	{
				JLabel lblResults = new JLabel("orders");
				lblResults.setFont(new Font("Tahoma", Font.PLAIN, 20));

				lblResults.setBounds(110, 11, 186, 25);
				add(lblResults);
									
				JButton btnBack = new JButton("Back");
				
				btnBack.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						clc.cgui.setContentPane(backPanel);
					}
				});
				btnBack.setBounds(490, 370, 100, 30);
				add(btnBack);
				
		
				
	}

	

	
	public void forlibrerian()
	{
		
		showResults();
		
		JLabel lblCopyId = new JLabel("Copy id");
		lblCopyId.setBounds(82, 397, 46, 14);
		add(lblCopyId);
		
		textField = new JTextField();
		textField.setBounds(177, 394, 86, 20);
		add(textField);
		textField.setColumns(10);
		
		JButton btnLoan = new JButton("loan");
		btnLoan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String copyId=textField.getText();
				int flag=0;
				
				for(int i=0;i<listOrderBookStudent1.size();i++)
					{
					if((Integer)table.getValueAt(i, 1)==Integer.parseInt(copyId))
					{		
						if(table.getValueAt(i,3).equals("wait"))
						{
							JOptionPane.showMessageDialog(new JFrame(),"The book is not in the library yet");	
							flag=1;
						}
					
					}
					}
				
				if(flag==0)
				{
				if(copyId.equals(""))
				{
					JOptionPane.showMessageDialog(new JFrame(),"There is an empty field");
				}
				else{
					
				String[] s={"new loan",rId,copyId,"orders"};
				try {
					
					clc.client.sendToServer(s);
					while(clc.client.flag==0)
						System.out.print("");
					clc.client.flag=0;
					JOptionPane.showMessageDialog(new JFrame(),clc.client.message);
					textField.setText(null);
					if(clc.client.message.equals("book loaned for 3 days")||clc.client.message.equals("book loaned for 14 days"))
						refresh();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
		}
			}
			}
		});
		
		btnLoan.setBounds(307, 393, 89, 23);
		add(btnLoan);
			
		
	}
	
	public void forStudent()
		{
	   JButton btnOrderBook  = new JButton("Order book");
				   btnOrderBook.addActionListener(new ActionListener() {
					   public void actionPerformed(ActionEvent arg0) {
						 		    	GUI_OrderBook ob= new GUI_OrderBook(clc,sob);
								    	clc.cgui.setContentPane(ob);
						 
					}});
				setLayout(null);
				btnOrderBook.setBounds(95, 370, 160, 30);
				this.add(btnOrderBook);
				showResults();
							
		}
			
			
	public void refresh()
	{
		 String[] s={"getuserName",rId};
		 try {
			clc.client.sendToServer(s);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while(clc.client.flag==0)
		     System.out.print("");
	    clc.client.flag=0;
	    String userName=(String) clc.client.message;
		
		String s2[]={"getListOrderBook",userName};
		try {
			clc.client.sendToServer(s2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				while(clc.client.flag==0)
				    System.out.print("");
	    clc.client.flag=0;
		listOrderBookStudent1 =((ListOrderBookStudent)clc.client.message).ListOrderBookStudent;
		if(listOrderBookStudent1.isEmpty())
		    {
		    JOptionPane.showMessageDialog(new JFrame(),"no books waiting");
		    }
		    else
		    {
		    clc.cgui.setContentPane(backPanel);
		    }
		
		
	}
		
			public void showResults() {
				
				
				JScrollPane scrollPane = new JScrollPane();
				scrollPane.setBounds(95, 70, 500, 300);
				add(scrollPane);
				
			
				
				table = new JTable(listOrderBookStudent1.size(),4);
				scrollPane.setViewportView(table);
							
				table.getColumnModel().getColumn(0).setHeaderValue("Name book");
				table.getColumnModel().getColumn(1).setHeaderValue("copyId");
				table.getColumnModel().getColumn(2).setHeaderValue("date");
				table.getColumnModel().getColumn(3).setHeaderValue("status");
				
				
				
				for(int i=0;i<listOrderBookStudent1.size();i++)
				{
					
						table.setValueAt(listOrderBookStudent1.get(i).getBookName(),i,0);
						table.setValueAt(listOrderBookStudent1.get(i).getCopyId(),i,1);
						table.setValueAt(listOrderBookStudent1.get(i).getDate(),i,2);
						table.setValueAt(listOrderBookStudent1.get(i).getStatus(),i,3);

				}
				
				
				
				
			
			}
}
